package com.gooddare.view.component 
{
	import com.gooddare.event.LyleffEvent;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.vo.RuleVO;
	import com.greensock.TweenMax;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import flash.display.Sprite;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class RuleGroupComponent extends BaseComponent
	{
		private static const pd:Number = 0.12;
		private var index:int = 0;
		private static var ruleNum:int = 15;//尺子上的个数 
		private var vo:RuleVO;
		private var ruleMenu:Sprite;
		
		private var ruleLength:Number;//尺子长度
		private var ruleItemWidth:Number = 70;//刻度原宽
		private var scaleNum:Number;//缩放比例
		private var ruleItemArr:Array;
		public function RuleGroupComponent(index:int = 0, vo:RuleVO = null ) 
		{
			this.index = index;
			this.vo = vo;
		}
		override public function initUI():void {
			ruleNum = vo.photolist.length - index * 15 > 15?15:vo.photolist.length - index * 15;
			
			ruleItemArr = new Array();
			ruleMenu = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_RULEMENU_UI) as Sprite;
			this.addChild(ruleMenu);
			this.y = 610;
			//TweenMax.to(this, 0.5, { x:120 } );
			ruleLength = int(ruleMenu.width - 2);
			if (ruleNum < 15) {
				ruleLength = ruleLength - 46 * (14 - ruleNum);
			}
			createItem();
		}
		
		private function createItem():void 
		{
			var itemLength:Number = ruleLength / ruleNum;//缩放后的宽度
			for (var i:int = 0; i < ruleNum; i++ ) {
				var ric:RuleItemComponent = new RuleItemComponent(i, vo.photolist[i+index*15]);
				ruleMenu.addChild(ric);
				ruleItemWidth = ric.width;
				ruleItemArr.push(ric);
				ric.addEventListener(LyleffEvent.EVENT_APP_RULEITEM_MOUSE_ROLL_OVER, __lyleffEventRollOverHandle);
				ric.addEventListener(LyleffEvent.EVENT_APP_RULEITEM_MOUSE_ROLL_OUT,__lyleffEventRollOutHandle)
				scaleNum = (itemLength * 100) / ruleItemWidth / 100;
				if (itemLength < ruleItemWidth) {
					ric.scaleX = ric.scaleY = scaleNum;
				}
				ric.x = (ric.width) * i;
			}
			
		}
		
		private function __lyleffEventRollOutHandle(e:LyleffEvent):void 
		{
			var itemLength:Number = ruleLength / ruleNum;//缩放后的宽度
			var itemScale:Number=(itemLength * 100) / ruleItemWidth / 100;
			var way:Number = 0;
			for (var i:int = 0; i < ruleNum; i++ ) {
				TweenMax.to(ruleItemArr[i], 0.5, { x:way } );
				way += itemLength;
				TweenMax.to(ruleItemArr[i], 0.5, { scaleX:itemScale, scaleY:itemScale } );
				ruleItemArr[i].roll(0);
			}
		}
		
		private function __lyleffEventRollOverHandle(e:LyleffEvent):void 
		{
			var wayArr:Array = new Array();
			var way:Number = 0;
			var touch:int = 0;
			var index:int = int(e.data);
			for (var k:int = 0; k < 5; k++ ) {
				if (ruleItemArr[index - 2 + k]) {//如果存在
					ruleItemArr[index - 2 + k].roll(Math.abs(Math.abs(2 - k) - 3));
				}
			}
			
			var advScaleNum:Number=ruleLength;//余下的长度；
			for (var i:int = 0; i < 5; i++ ) {
				if (ruleItemArr[index - 2 + i]) {//如果存在
					var sn:Number = 1-Math.abs(i - 2)*pd;
					TweenMax.to(ruleItemArr[index - 2 + i], 0.5, { scaleX:sn, scaleY:sn } );
					touch++;
					advScaleNum -= ruleItemWidth * sn;
				}
			}
			advScaleNum /= ruleNum-touch;
			var advScale:Number = advScaleNum*100 / ruleItemWidth/100;
			
			for (var j:int = 0; j < ruleNum; j++ ) {
				if (j >= index - 2 && j <= index + 2) {
					if (index == j) {
						wayArr[j]=ruleItemWidth;
					}else if (Math.abs(index - j) == 1) {
						wayArr[j]=ruleItemWidth*(1-pd);
					}else if (Math.abs(index - j) == 2) {
						wayArr[j]=ruleItemWidth*(1-pd*2);
					}
				}else {
					wayArr[j] = advScaleNum;
					TweenMax.to(ruleItemArr[j], 0.5, { scaleX:advScale, scaleY:advScale } );
				}
				
			}
			var myWay:Number = 0;
			for (var l:int = 0; l < ruleNum; l++ ) {
				TweenMax.to(ruleItemArr[l], 0.5, { x:myWay } );
				myWay += wayArr[l];
			}
		}
	}

}